package com.zf.bm.common.utils;


import com.zf.bm.common.exception.SystemException;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * cache 工具类
 * @author shengkai
 * @since 2025/06/23
 */
public class CacheUtils {

    private static EhCacheManager cacheManager = SpringContextHolder.getBean(EhCacheManager.class);
    private static final String SYS_CACHE = "sysCache";
    private static final String SYS_TOKEN = "sys_token";
    public static final String CITY_JSON = "cityJson";
    //密码重试次数
    private static final String PASSWORDRETRYCACHE = "passwordRetryCache";

    private CacheUtils(){}

    /**
     * 获取SYS_CACHE缓存
     */
    public static Object get(String key){return get(SYS_CACHE,key);}

    /**
     * 根据key获取token值
     */
    public static Object getToken(String key){return get(SYS_TOKEN,key);}

    /**
     * 获取所有tonken值
     */
    public static Cache getTokens(){return getCache(SYS_TOKEN);}

    /**
     * 获取SYS_CACHE
     */
    public static Object get(String key,Object defaultValue){
        Object value = get(key);
        return value != null ? value:defaultValue;
    }

    /**
     * 根据指定的key，value 插入sys_cache缓存中
     */
    public static void put(String key,Object value){
        put(SYS_CACHE, key,value);
    }

    /**
     * 将指定的key，value，插入sys_cache缓存中
     */
    public static void putToken(String key,Object value){put(SYS_TOKEN,key,value);}

    /**
     * 根据指定的key，移除sys_cache缓存
     */
    public static void remove(String key){remove(SYS_CACHE,key);}

    /**
     * 根据指定的key，移除sys_cache缓存
     */
    public static void removeToken(String key){remove(SYS_TOKEN,key);}

    /**
     * 根据缓存名称，获取指定key的值
     */
    public static Object get(String cacheName,String key){return getCache(cacheName).get(key);}

    /**
     * 根据缓存名称，获取指定key的值，将值对比
     */
    public static Object get(String cacheName,String key,Object defaultValue){
        Object value = get(cacheName, key);
        return value != null ? value :defaultValue;
    }

    /**
     * 将指定的key,value 插入指定的缓存中
     */
    public static void put(String cacheName, String key, Object value) {
        Cache<String, Object> cache = getCache(cacheName);
        cache.put(key, value);
    }

    /**
     * 根据指定的缓存名称，清除key
     */
    public static void remove(String cacheName,String key){getCache(cacheName).remove(key);}




    /**
     * 获取并增加密码重试次数
     */
    public static int incPasswdRetryCount(String account) {
        Cache<String, Integer> cache = cacheManager.getCache(PASSWORDRETRYCACHE);
        synchronized (cacheManager) {
            Integer count = cache.get(account);
            if (null == count)
                count = 0;
            cache.put(account, ++count);
            return count;
        }
    }

    /**
     * 清除密码重试次数
     */
    public static void cleanPasswdRetryCount(String account) {
        Cache<String, AtomicInteger> cache = cacheManager.getCache(PASSWORDRETRYCACHE);
        cache.remove(account);
    }

    /**
     * 获取一个cache,不存在则显示异常
     */
    public static Cache<String, Object> getCache(String cacheName) {
        Cache<String, Object> cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            throw new SystemException("当前系统中没有定义" + cacheName + "这个缓存");
        }
        return cache;
    }

}
